﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using PPWMS.DBUtility;
using PPWMS.Model;
namespace PPWMS.DAL
{
	/// <summary>
	/// 数据访问类:CEMap
	/// </summary>
	public partial class CEMap
	{
		public CEMap()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return SQLHelper.GetMaxID("MapId", "tb_CEMap"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int MapId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from tb_CEMap");
			strSql.Append(" where MapId=@MapId");
			SqlParameter[] parameters = {
					new SqlParameter("@MapId", SqlDbType.Int,4)
			};
			parameters[0].Value = MapId;

            return SQLHelper.Exists(strSql.ToString(), parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(PPWMS.Model.CEMapModel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into tb_CEMap(");
			strSql.Append("NameCn,NameEn)");
			strSql.Append(" values (");
			strSql.Append("@NameCn,@NameEn)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@NameCn", SqlDbType.NVarChar,50),
					new SqlParameter("@NameEn", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.NameCn;
			parameters[1].Value = model.NameEn;

            object obj = SQLHelper.GetSingle(strSql.ToString(), parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(PPWMS.Model.CEMapModel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tb_CEMap set ");
			strSql.Append("NameCn=@NameCn,");
			strSql.Append("NameEn=@NameEn");
			strSql.Append(" where MapId=@MapId");
			SqlParameter[] parameters = {
					new SqlParameter("@NameCn", SqlDbType.NVarChar,50),
					new SqlParameter("@NameEn", SqlDbType.NVarChar,50),
					new SqlParameter("@MapId", SqlDbType.Int,4)};
			parameters[0].Value = model.NameCn;
			parameters[1].Value = model.NameEn;
			parameters[2].Value = model.MapId;

            int rows = SQLHelper.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int MapId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_CEMap ");
			strSql.Append(" where MapId=@MapId");
			SqlParameter[] parameters = {
					new SqlParameter("@MapId", SqlDbType.Int,4)
			};
			parameters[0].Value = MapId;

            int rows = SQLHelper.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string MapIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_CEMap ");
			strSql.Append(" where MapId in ("+MapIdlist + ")  ");
            int rows = SQLHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public PPWMS.Model.CEMapModel GetModel(int MapId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 MapId,NameCn,NameEn from tb_CEMap ");
			strSql.Append(" where MapId=@MapId");
			SqlParameter[] parameters = {
					new SqlParameter("@MapId", SqlDbType.Int,4)
			};
			parameters[0].Value = MapId;

			PPWMS.Model.CEMapModel model=new PPWMS.Model.CEMapModel();
            DataSet ds = SQLHelper.Query(strSql.ToString(), parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PPWMS.Model.CEMapModel DataRowToModel(DataRow row)
        {
            PPWMS.Model.CEMapModel model = new PPWMS.Model.CEMapModel();
            if (row != null)
            {
                if (row["MapId"] != null && row["MapId"].ToString() != "")
                {
                    model.MapId = int.Parse(row["MapId"].ToString());
                }
                if (row["NameCn"] != null)
                {
                    model.NameCn = row["NameCn"].ToString();
                }
                if (row["NameEn"] != null)
                {
                    model.NameEn = row["NameEn"].ToString();
                }
            }
            return model;
        }

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select MapId '编号',NameCn '中文',NameEn '英文' ");
			strSql.Append(" FROM tb_CEMap ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            return SQLHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" MapId,NameCn,NameEn ");
			strSql.Append(" FROM tb_CEMap ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
            return SQLHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tb_CEMap ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = SQLHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.MapId desc");
			}
			strSql.Append(")AS Row, T.*  from tb_CEMap T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return SQLHelper.Query(strSql.ToString());
		}
        #endregion  BasicMethod
        
    }
}

